<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Pasien extends zulController {
	public $parameter	= "";
	function __construct(){
		parent::__construct();
		$this->load->model("PasienModel");
		$this->checkLogin();
		
	}
	public function index()
	{
		$colModel['NO'] = array('No', 20, TRUE, 'center', 0);
        $colModel['NO_REG'] = array('Nomor Regristasi', 110, FALSE, 'center', 1);
		$colModel['NAMA_PASIEN'] = array('Nama Pasien', 110, FALSE, 'center', 1);
		$colModel['ALAMAT'] = array('Alamat', 250, FALSE, 'center', 1);
		$colModel['LIHAT_DETAIL'] = array('Lihat Detail', 250, FALSE, 'center', 1);
		$colModel['EDIT'] = array('Edit', 70, FALSE, 'center', 0);
        $gridParams = array(
            'width' => 'auto',
            'height' => '390',
            'rp' => 15,
            'rpOptions' => '[10,15,20,25,40]',
            'pagestat' => 'Displaying: {from} to {to} of {total} items.',
            'blockOpacity' => 0.5,
            'title' => 'Daftar Pasien'
        );
        $buttons[] = array('Tambah', 'add', 'pengujian_aba');
        $buttons[] = array('Hapus', 'delete', 'pengujian_aba');
        $buttons[] = array('separator');
        $buttons[] = array('Pilih Semua', 'add', 'pengujian_aba');
        $buttons[] = array('Hapus Pilihan', 'delete', 'pengujian_aba');
        $buttons[] = array('separator');
		
        $grid_js = build_grid_js('flex1', site_url("Pasien/gridPasien"), $colModel, 'ID_PASIEN', 'ASC', $gridParams, $buttons);

        $data['title'] = "Daftar Pasien";
        $data['info'] = "Daftar Pasien";
        $data['js_grid'] = $grid_js;


        $data['added_js'] =
                "<script type='text/javascript'>
			function pengujian_aba(com,grid)
			{
				if (com=='Pilih Semua')
				{
					$('.bDiv tbody tr',grid).addClass('trSelected');
				}
				
				if (com=='Hapus Pilihan')
				{
					$('.bDiv tbody tr',grid).removeClass('trSelected');
				}
				
				if (com=='Tambah')
				{
					location.href='" . base_url() . "index.php/Pasien/formPasien/'; 
				}
				
				if (com=='Hapus')
				{
				   if($('.trSelected',grid).length>0){
					   if(confirm('Anda yakin ingin mengaktifkan ' + $('.trSelected',grid).length + ' buah data?')){
							var items = $('.trSelected',grid);
							var itemlist ='';
							for(i=0;i<items.length;i++){
								itemlist+= items[i].id.substr(3)+',';
							}
							$.ajax({
							   type: 'POST',
							   url: '" .base_url('index.php/Pasien/deletePasien')."',
							   data: 'items='+itemlist,
							   success: function(data){
								$('#flex1').flexReload();
								alert(data);
							   }
							});
						}
					} else {
						return false;
					} 
				}
			} 
			</script>
			";
		$data["active"] = "master";
		$data["content"]	= $this->load->view("grid",$data,true);
		$this->load->view("main_new",$data);
	}
	function gridPasien(){
		$valid_fields = array('ID_PASIEN', 'NAMA_PASIEN', 'ALAMAT', 'TGL_DAFTAR', 'JENIS_KELAMIN', 'TGL_LAHIR');
        $this->flexigrid->validate_post('ID_PASIEN', 'ASC', $valid_fields);
        $records = $this->PasienModel->gridPasien();
        $this->output->set_header($this->config->item('json_header'));
        $no = 1;
        foreach ($records['records']->result() as $row) {
            $record_items[] = array($row->ID_PASIEN,
                $no,
				$row->NO_REG,
				$row->NAMA_PASIEN,
				str_replace("<p>","</p>",$row->ALAMAT),
				"<a class='fancybox fancybox.ajax' href='".site_url("/Pasien/viewPasien/".$row->ID_PASIEN)."'><img src='". base_url()."assets/images/grid/magnifier.png'></a>",
				"<a href='".site_url("/Pasien/formPasien/".$row->ID_PASIEN)."'><img src='". base_url()."assets/images/grid/edit.png'></a>"
            );
            $no++;
        }
        if (isset($record_items))
            $this->output->set_output($this->flexigrid->json_build($records['record_count'], $record_items));
        else
            $this->output->set_output('{"page":"1","total":"0","rows":[]}');
	}
	public function formPasien($parameter=FALSE){
		$this->load->library("form_validation");
		$this->form_validation->set_error_delimiters('<div class="alert alert-error fade in"><button type="button" class="close" data-dismiss="alert">&times;</button>', '</div>');
		$this->form_validation->set_rules(array(
		array('field'=>'no_reg','label'=>'Nomor Registrasi','rules'=>'required|callback_checkReg'),
		array('field'=>'nama_pasien','label'=>'Nama Pasien','rules'=>'required'),
		array('field'=>'alamat','label'=>'Alamat','rules'=>'required'),
		array('field'=>'tgl_daftar','label'=>'Tanggal Daftar','rules'=>'required'),
		array('field'=>'jenis_kelamin','label'=>'Jenis Kelamin','rules'=>'required'),
		array('field'=>'tgl_lahir','label'=>'Tanggal Lahir','rules'=>'required'),
		));
		$this->form_validation->set_message('required','Kolom %s harus diisi !');
		$this->session->set_userdata("parameter",$parameter);
		if($this->form_validation->run() == FALSE){
			if($parameter == TRUE){
				$data["value"] = $this->PasienModel->get($parameter);
			}
			$data["parameter"]	= $parameter;
			$data["active"] = "master";
			$data["content"]	= $this->load->view("formPasien",$data,true);
			$this->load->view("main_new",$data);
		} else {
			$no_reg	= $this->input->post("no_reg");
			$pasien	= $this->input->post("nama_pasien");
			$alamat	= $this->input->post("alamat");
			$tgl_daftar	= $this->input->post("tgl_daftar");
			$jenkel	= $this->input->post("jenis_kelamin");
			$tgl_lahir	= $this->input->post("tgl_lahir");
			$data	= array(
			"NO_REG"=>$no_reg,
			"NAMA_PASIEN"=>$pasien,
			"ALAMAT"=>$alamat,
			"TGL_DAFTAR"=>$tgl_daftar,
			"JENIS_KELAMIN"=>$jenkel,
			"TGL_LAHIR"=>$tgl_lahir,
			);
			$this->PasienModel->save($data, $parameter);
			redirect("/Pasien");
		}
	}
	function viewPasien($parameter){
		$data["value"] 		= $this->PasienModel->get($parameter);
		$data["tgl_daftar"]	= $this->dateFormat($data["value"]["TGL_DAFTAR"]);
		$data["tgl_lahir"]	= $this->dateFormat($data["value"]["TGL_LAHIR"]);
		$this->load->view("kartuPasien",$data);
	}
	function checkReg($parameter){
		$result	= $this->PasienModel->get_by("NO_REG",$parameter,NULL,TRUE);
		$id		= $this->session->userdata("parameter");
		if(count($result) > 0){
			if($id == $result["ID_PASIEN"] && $parameter == $result["NO_REG"]){
				$this->session->set_userdata("parameter",NULL);
				return TRUE;
			} else {
				$this->form_validation->set_message("checkReg","Nomor Registrasi sudah ada !");
				return FALSE;
			}
		} else {
			$this->session->set_userdata("parameter",NULL);
			return TRUE;
		}
	}
	function deletePasien(){
		$ids	= $this->input->post('items');
		$this->PasienModel->delete($ids);
		$msg ='Data yang anda pilih berhasil dihapus';
		$this->output->set_output($msg);
	}
}